package com.ruoyi.biz.controller;

import com.ruoyi.collect.domain.CollectionGroupStats;
import com.ruoyi.collect.domain.RecipeFavorites;
import com.ruoyi.collect.domain.CollectRecipes;
import com.ruoyi.collect.service.IRecipeFavoritesService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 菜谱收藏Controller
 * 
 * @author ruoyi
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/collect/favorites")
public class RecipeFavoritesController extends BaseController
{
    @Autowired
    private IRecipeFavoritesService recipeFavoritesService;

    /**
     * 查询菜谱收藏列表
     */
    @PreAuthorize("@ss.hasPermi('collect:favorites:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecipeFavorites recipeFavorites)
    {
        startPage();
        List<RecipeFavorites> list = recipeFavoritesService.selectRecipeFavoritesList(recipeFavorites);
        return getDataTable(list);
    }

    /**
     * 导出菜谱收藏列表
     */
    @PreAuthorize("@ss.hasPermi('collect:favorites:export')")
    @Log(title = "菜谱收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecipeFavorites recipeFavorites)
    {
        List<RecipeFavorites> list = recipeFavoritesService.selectRecipeFavoritesList(recipeFavorites);
        ExcelUtil<RecipeFavorites> util = new ExcelUtil<RecipeFavorites>(RecipeFavorites.class);
        util.exportExcel(response, list, "菜谱收藏数据");
    }

    /**
     * 获取菜谱收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('collect:favorites:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(recipeFavoritesService.selectRecipeFavoritesById(id));
    }

    /**
     * 新增菜谱收藏
     */
    @PreAuthorize("@ss.hasPermi('collect:favorites:add')")
    @Log(title = "菜谱收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecipeFavorites recipeFavorites)
    {
        return toAjax(recipeFavoritesService.insertRecipeFavorites(recipeFavorites));
    }

    /**
     * 修改菜谱收藏
     */
    @PreAuthorize("@ss.hasPermi('collect:favorites:edit')")
    @Log(title = "菜谱收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecipeFavorites recipeFavorites)
    {
        return toAjax(recipeFavoritesService.updateRecipeFavorites(recipeFavorites));
    }

    /**
     * 删除菜谱收藏
     */
    @PreAuthorize("@ss.hasPermi('collect:favorites:remove')")
    @Log(title = "菜谱收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(recipeFavoritesService.deleteRecipeFavoritesByIds(ids));
    }

    @GetMapping("/select")
    public List<RecipeFavorites> select(){
        return recipeFavoritesService.findBatch();
    }

    @GetMapping("/select/{id}")
    public RecipeFavorites selectById(@PathVariable("id") String id){
        return recipeFavoritesService.findById(id);
    }

    @GetMapping("/select/page")
     public Object findPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize,
                            RecipeFavorites recipeFavorites) {
        return recipeFavoritesService.findByPage(pageNum, pageSize, recipeFavorites);
    }

    @DeleteMapping("/delete")
    public String delete(@RequestBody Integer[] ids) {
        return recipeFavoritesService.removeBatch(ids)> 0 ? "批量删除成功" : "批量删除失败";
    }

    @DeleteMapping("/delect/{id}")
    public String delect(@PathVariable("id") Integer id) {
        return recipeFavoritesService.remove(id) > 0 ? "新增成功" : "新增失败";
    }

    /**
     * 根据用户ID查询收藏的菜谱列表
     */
    @Anonymous
    @GetMapping("/user/{userId}")
    public AjaxResult getFavoritesByUserId(@PathVariable("userId") String userId) {
        List<RecipeFavorites> list = recipeFavoritesService.selectRecipeFavoritesByUserId(userId);
        return success(list);
    }

    /**
     * 根据用户ID查询收藏夹分组统计信息
     */
    @Anonymous
    @GetMapping("/collections/user/{userId}")
    public AjaxResult getCollectionGroupStatsByUserId(@PathVariable("userId") String userId) {
        List<CollectionGroupStats> list = recipeFavoritesService.selectCollectionGroupStatsByUserId(userId);
        return success(list);
    }

    /**
     * 随机返回一条菜谱数据
     */
    @Anonymous
    @GetMapping("/random")
    public AjaxResult getRandomRecipe() {
        CollectRecipes recipe = recipeFavoritesService.getRandomRecipe();
        return success(recipe);
    }

    /**
     * 随机返回一条菜谱数据（包含烹饪时间和难度名称）
     */
    @Anonymous
    @GetMapping("/random/details")
    public AjaxResult getRandomRecipeWithDetails() {
        CollectRecipes recipe = recipeFavoritesService.getRandomRecipeWithDetails();
        return success(recipe);
    }

    /**
     * 随机返回两条菜谱数据（包含烹饪时间和难度名称）
     */
    @Anonymous
    @GetMapping("/random/details/list")
    public AjaxResult getRandomRecipesWithDetails() {
        List<CollectRecipes> recipes = recipeFavoritesService.getRandomRecipesWithDetails();
        return success(recipes);
    }

    /**
     * 根据收藏夹ID查询菜谱列表
     */
    @Anonymous
    @GetMapping("/collections/recipes/{collectionsId}")
    public AjaxResult getRecipesByCollectionsId(@PathVariable("collectionsId") String collectionsId) {
        List<CollectRecipes> recipes = recipeFavoritesService.getRecipesByCollectionsId(collectionsId);
        return success(recipes);
    }

}
